_Pragma("once");

#include "DummyChannel.h"


class SerialChannel : public DummyChannel {
public:
    /** Default constructor */
    SerialChannel(const int channel, DeviceCommunicator& communicator) : DummyChannel(channel, communicator) {
    }

    /** Default destructor */
    ~SerialChannel() {
    }

    ssize_t read(std::string& result) override {
        std::unique_lock<Mutex> locker(mallChannelMutex);
        ssize_t returnCode = ::write(icSelector.getDeviceFd(), &mchannel, sizeof (mchannel));//deviceFd
        if (0 < returnCode) {
            returnCode = DummyChannel::read(result);
        } else {
            BUILD_EXCEPTION_ERROR(FirmwareError::CHANNEL_COMM_ERROR, returnCode, errno, mexceptionTip);
        }
        locker.unlock();

        return returnCode;
    }

    ssize_t write(const std::string& data) override {
        std::unique_lock<Mutex> locker(mallChannelMutex);
        LOG_DEDUG("%s,begin write channel", mexceptionTip.data());
        ssize_t result = ::write(icSelector.getDeviceFd(), &mchannel, sizeof (mchannel));
        if (0 < result) {
            LOG_DEDUG("%s,begin write serial data", mexceptionTip.data());
            result = DummyChannel::write(data);
        } else {
            BUILD_EXCEPTION_ERROR(FirmwareError::CHANNEL_COMM_ERROR, result, errno, mexceptionTip);
        }
        locker.unlock();

        return result;
    }


    virtual std::string& sendStringReturnString(const std::string& parameter, std::string& resultStr) override;


    static DummyChannel::Mutex mallChannelMutex;//std::mutex
protected:
    static BasicDevice icSelector;

};



//typedef  SerialChannel<SerialPortCommunicator> InnerSerialChannel;


